package com.remy;

import com.remy.order.OrderRecordList;
import com.remy.settings.Settings;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {


	/** Called when the activity is first created. */
	public TabHost tabhost;
	
	
	public static final String TAB_FIRST = "TAB_FIRST";
	public static final String TAB_SECOND = "TAB_SECOND";
	public static final String TAB_THIRD = "TAB_THIRD";
	public RadioGroup radioGroup;
	public RadioButton radio_button_0;
	public Drawable drawableTop ;
	public static final String ORDER_SUCCESSED =  
	        "com.android.broadcast.ORDER_SUCCESSED";
	
	public static final String ORDER_NOTIFY_STATUS_CHANGE_TO_NORMAL =  
	        "com.android.broadcast.ORDER_NOTIFY_STATUS_CHANGE_TO_NORMAL";
	
	
	private static final String TAG = "MainActivity" ;
	
	private IntentFilter filter1 ;
	private IntentFilter filter2;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mtab);
        init();
    }
    
	@Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        
        super.onResume();Log.e(TAG,"------------->onStop");
        //unregisterReceiver(myReceiver1);
        //unregisterReceiver(myReceiver2);
    }   
	
    @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();Log.e(TAG,"------------->onResume");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();Log.e(TAG,"------------->onPause");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();Log.e(TAG,"------------->onDestroy");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();Log.e(TAG,"------------->onStart");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();Log.e(TAG,"------------->onRestart");
	}

	private void init(){
		filter1 = new IntentFilter();
		filter1.addAction(ORDER_SUCCESSED);
		//filter1.setPriority(Integer.MAX_VALUE);
		registerReceiver(myReceiver1, filter1);
		
		filter2 = new IntentFilter();
		filter2.addAction(ORDER_NOTIFY_STATUS_CHANGE_TO_NORMAL);
		//filter2.setPriority(Integer.MAX_VALUE);
		registerReceiver(myReceiver2, filter2);

		drawableTop = getResources().getDrawable(R.drawable.ic_tab_order);
		tabhost = getTabHost();
		tabhost.setup();

		TabSpec ts1 = tabhost.newTabSpec(TAB_FIRST).setIndicator(TAB_FIRST);
		ts1.setContent(new Intent(MainActivity.this, OrderRecordList.class));
		tabhost.addTab(ts1);

		TabSpec ts2 = tabhost.newTabSpec(TAB_SECOND).setIndicator(TAB_SECOND);
		ts2.setContent(new Intent(MainActivity.this, LocationOverlay.class));
		tabhost.addTab(ts2);

		TabSpec ts3 = tabhost.newTabSpec(TAB_THIRD).setIndicator(TAB_THIRD);
		ts3.setContent(new Intent(MainActivity.this, Settings.class));
		tabhost.addTab(ts3);

		tabhost.clearFocus();
		tabhost.setCurrentTab(1);

		radioGroup = (RadioGroup) findViewById(R.id.main_radio);
		radioGroup
				.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);

		radio_button_0 = (RadioButton) findViewById(R.id.radio_button0);
		radio_button_0.setCompoundDrawablesWithIntrinsicBounds(null,
				drawableTop, null, null);
          
	}
	
    private OnCheckedChangeListener radioGroupOnCheckedChangeListener = new OnCheckedChangeListener(){

    	public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			
			switch(checkedId){
			case R.id.radio_button0:
				tabhost.setCurrentTabByTag(TAB_FIRST);
				break;
			case R.id.radio_button1:
				tabhost.setCurrentTabByTag(TAB_SECOND);
				break;
			case R.id.radio_button2:
				tabhost.setCurrentTabByTag(TAB_THIRD);
				break;
			}
		}
    	
    };
    
	private BroadcastReceiver myReceiver1 = new BroadcastReceiver() { 
		
        @Override 
        public void onReceive(Context context, Intent intent) { 
            drawableTop = getResources().getDrawable(R.drawable.ic_tab_order_new);
            radio_button_0.setCompoundDrawablesWithIntrinsicBounds(null,
    				drawableTop, null, null);
        } 
        
 
    }; 
    
    private BroadcastReceiver myReceiver2 = new BroadcastReceiver() { 
		 
        @Override 
        public void onReceive(Context context, Intent intent) { 
            drawableTop = getResources().getDrawable(R.drawable.ic_tab_order);
            radio_button_0.setCompoundDrawablesWithIntrinsicBounds(null,
    				drawableTop, null, null);
        } 
 
    }; 
    
}